package com.demo.camunda.service.impl;

import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.TaskEntity;
import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
import org.camunda.bpm.engine.management.ActivityStatistics;
import org.camunda.bpm.engine.runtime.ExecutionQuery;
import org.camunda.bpm.engine.task.TaskQuery;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.Activity;
import org.camunda.bpm.model.bpmn.instance.MultiInstanceLoopCharacteristics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service
public class AddMultiInstance {

    private static Logger logger = LoggerFactory.getLogger(AddMultiInstance.class);
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private ManagementService managementService;
    @Resource
    private TaskService taskService;
    @Resource
    private ProcessEngine processEngine;
    @Resource
    private IdentityService identityService;



    public Object execute(String processInstanceId) {
        //根据任务id获取任务实例
        TaskQuery taskQuery = taskService.createTaskQuery().processInstanceId(processInstanceId);
        //根据执行实例ID获取当前执行实例
        ExecutionQuery executionQuery = runtimeService.createExecutionQuery().processInstanceId(processInstanceId);

//        // 获取流程执行实例（即当前执行实例的父实例）
//        ExecutionEntity parentExecutionEntity = executionQuery
//        //判断当前执行实例的节点是否是多实例节点
//        InputStream processModel = ProcessEngines
//                .getDefaultProcessEngine()
//                .getRepositoryService()
//                .getProcessModel(multiExecutionEntity.getProcessDefinitionId());
//        BpmnModelInstance bpmnModelInstance = Bpmn.readModelFromStream(processModel);
//        Activity activity = bpmnModelInstance.getModelElementById(multiExecutionEntity.getCurrentActivityId());
//        MultiInstanceLoopCharacteristics loopCharacteristics  = (MultiInstanceLoopCharacteristics) activity.getLoopCharacteristics();
//        if(loopCharacteristics == null){
//            logger.error("此节点不是多实例节点");
//        }
//        //判断是否是并行多实例
//        if(loopCharacteristics.isSequential()){
//            logger.error("此节点为串行节点");
//        }
//        //创建新的子实例
//        ExecutionEntity execution = multiExecutionEntity.createExecution();
//        //获取并为新的执行实例设置当前活动节点
//        ActivityImpl activity1 = execution.getActivity();
//        //设置处理人
//        currentFlowElement.setAssignee(addUserTaskAssign);
//        childExecution.setCurrentFlowElement(currentFlowElement);
//        //获取设置变量
//        Integer nrOfInstances = (Integer) parentExecutionEntity.getVariableLocal(NUMBER_OF_INSTANCES);
//        Integer nrOfActiveInstances = (Integer) parentExecutionEntity.getVariableLocal(NUMBER_OF_ACTIVE_INSTANCES);
//        parentExecutionEntity.setVariableLocal(NUMBER_OF_INSTANCES,nrOfInstances+1);
//        parentExecutionEntity.setVariableLocal(NUMBER_OF_ACTIVE_INSTANCES,nrOfActiveInstances+1);
//        //通知活动开始
//        HistoryManager historyManager = commandContext.getHistoryManager();
//        historyManager.recordActivityStart(childExecution);
//        //获取处理行为类
//        ParallelMultiInstanceBehavior prallelMultiInstanceBehavior = (ParallelMultiInstanceBehavior) miActivityElement.getBehavior();
//        AbstractBpmnActivityBehavior innerActivityBehavior = prallelMultiInstanceBehavior.getInnerActivityBehavior();
//        //执行
//        innerActivityBehavior.execute(childExecution);
        return null;
    }
}
